home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 March: Reference Library / Dev.CD Mar 96 RL / Dev.CD Mar 96 RL.toast / Technical Documentation / develop / develop Issue 25 / develop Issue 25 code / QD3D to QTVR / ArticleCode / Source / camera.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-21  |  3.1 KB  |  97 lines  |  [TEXT/MPCC]

  1. #include <FixMath.h>
  2. #include "QD3DtoQTVR.h"
  3. #include "extern.h"
  4. #include "camera.h"
  5.  
  6. TQ3CameraObject MyNewCamera(CWindowPtr theWindow)
  7. {
  8.     TQ3ViewAngleAspectCameraData    perspectiveData;
  9.     TQ3CameraObject                    camera;
  10.     // Set the field of view to 30 degrees (or 30*kQ3Pi/180 radians).
  11.     float                             fieldOfView = .52359333333;
  12.     TQ3Status                        returnVal = kQ3Failure ;
  13.  
  14.     // Assign default placement and range
  15.     perspectiveData.cameraData.placement.cameraLocation     = kMyDefaultFrom;
  16.     perspectiveData.cameraData.placement.pointOfInterest     = kMyDefaultTo;
  17.     perspectiveData.cameraData.placement.upVector             = kMyDefaultUp;
  18.     perspectiveData.cameraData.range.hither    = kMyDefaultHither;
  19.     perspectiveData.cameraData.range.yon     = kMyDefaultYon;
  20.  
  21.     // Assign standard viewport
  22.     perspectiveData.cameraData.viewPort.origin.x = -1.0;
  23.     perspectiveData.cameraData.viewPort.origin.y = 1.0;
  24.     perspectiveData.cameraData.viewPort.width = 2.0;
  25.     perspectiveData.cameraData.viewPort.height = 2.0;
  26.     
  27.     perspectiveData.fov                = fieldOfView;
  28.     perspectiveData.aspectRatioXToY    =
  29.         (float) (theWindow->portRect.right - theWindow->portRect.left) / 
  30.         (float) (theWindow->portRect.bottom - theWindow->portRect.top);
  31.         
  32.     camera = Q3ViewAngleAspectCamera_New(&perspectiveData);
  33.     
  34.     return camera ;
  35. }
  36.  
  37.  
  38. void MyGetBoundingSphere(TQ3ViewObject viewObject, TQ3GroupObject mainGroup, TQ3BoundingSphere *viewBSphere)
  39. {
  40.     TQ3Status                    status;
  41.     
  42.     Q3View_StartBoundingSphere(viewObject,kQ3ComputeBoundsApproximate);
  43.     do {
  44.         status = Q3DisplayGroup_Submit(mainGroup, viewObject);
  45.     } while (Q3View_EndBoundingSphere(viewObject, viewBSphere) == kQ3ViewStatusRetraverse);                                        
  46. }
  47.  
  48.  
  49. void MyInitObjCamera(DocumentPtr theDocument)
  50. {
  51.     TQ3BoundingSphere             viewBSphere;
  52.     float                        viewRadius;
  53.     
  54.     if(theDocument == NULL)
  55.         return;
  56.         
  57.     // Get the bounding sphere of the drawable group (the entire model) in the view.
  58.     MyGetBoundingSphere(theDocument->theView, theDocument->documentGroup, &viewBSphere);
  59.  
  60.     // Get the bounding sphere's center and radius
  61.     theDocument->documentGroupCenter = viewBSphere.origin;
  62.     viewRadius = viewBSphere.radius;
  63.     
  64.     // position camera down z axis from bounding sphere based on center and radius
  65.     MyPlaceCamera(theDocument, theDocument->documentGroupCenter.x,theDocument->documentGroupCenter.y,
  66.                 theDocument->documentGroupCenter.z + kMyDistanceFactor*viewRadius + 1.0,
  67.                 theDocument->documentGroupCenter.x,theDocument->documentGroupCenter.y,
  68.                 theDocument->documentGroupCenter.z + kMyDistanceFactor*viewRadius);    
  69. }
  70.  
  71.  
  72. void    MyPlaceCamera(DocumentPtr theDocument, float fromX,float fromY,float fromZ,
  73.                     float toX,float toY,float toZ)
  74. {
  75.     TQ3CameraObject        camera;
  76.     TQ3CameraPlacement    cameraPos;
  77.     
  78.     Q3View_GetCamera(theDocument->theView, &camera );
  79.     Q3Camera_GetPlacement(camera, &cameraPos);
  80.     
  81.     cameraPos.cameraLocation.x = fromX;
  82.     cameraPos.cameraLocation.y = fromY;
  83.     cameraPos.cameraLocation.z = fromZ;
  84.     
  85.     cameraPos.pointOfInterest.x = toX;
  86.     cameraPos.pointOfInterest.y = toY;
  87.     cameraPos.pointOfInterest.z = toZ;
  88.     
  89.     Q3Camera_SetPlacement(camera, &cameraPos);
  90.     Q3View_SetCamera(theDocument->theView, camera );
  91.     Q3Object_Dispose(camera);
  92. }
  93.  
  94.  
  95.  
  96.  
  97.